package cn.edu.scau.cmi.huangmaoqin.adapter;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.util.ArrayList;
import java.util.List;

import javafx.event.ActionEvent;
import javafx.scene.control.TextArea;
import javafx.scene.control.Alert.AlertType;

public class AdapterController {
	@FXML
	private TextArea output1;
	@FXML
	private TextArea output2;
	@FXML
	private Button btn;
	@FXML
	private TextField idText;
	@FXML
	private TextField nameText;

	List<Integer> studentID = new ArrayList<Integer>();
	List<Student> students = new ArrayList<Student>();

	// Event Listener on Button[#btn2].onAction
	@FXML
	public void btnEvent(ActionEvent event) {
		Alert alert = new Alert(AlertType.INFORMATION);
		if (idText.getText().isEmpty() || nameText.getText().isEmpty()) {
			alert.headerTextProperty().set("请先完整输入学生的信息");
			alert.showAndWait();

		} else if (studentID.contains(Integer.parseInt(idText.getText()))) {
			alert.headerTextProperty().set("该学号已存在");
			alert.showAndWait();
		} else {
			output1.clear();
			output2.clear();
			Integer id = Integer.parseInt(idText.getText());
			String name = nameText.getText();
			Student student = new Student(id, name);
			studentID.add(id);
			students.add(student);
			Target adapterOfClass = new AdapterOfClass();
			studentID = adapterOfClass.getSortList(studentID);

			for (int i = 0; i < studentID.size(); i++) {
				for (int j = 0; j < students.size(); j++) {
					if (students.get(j).getId() == studentID.get(i)) {
						output1.appendText(studentID.get(i) + " " + students.get(j).getName() + "\n");
					}
				}
			}
			Target adapterOfObject = new AdapterOfObject();
			studentID = adapterOfObject.getSortList(studentID);
			for (int i = 0; i < studentID.size(); i++) {
				for (int j = 0; j < students.size(); j++) {
					if (students.get(j).getId() == studentID.get(i)) {
						output2.appendText(studentID.get(i) + " " + students.get(j).getName() + "\n");
					}
				}
			}
			idText.clear();
			nameText.clear();
		}
	}
}
